//
//  HMExpressionBottomView.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 2017/2/14.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit

enum HMExpressionBottomBtnType: Int {

    case recent = 10
    case normal = 11
    case emoji = 12
    case lxh = 13
    
}
                            // 容器视图
class HMExpressionBottomView: UIStackView {

    
    var selectdBtn:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setupUI() -> Void {
        
//        backgroundColor = UIColor.darkGray
        //设置布局方式
//        axis = .horizontal
//        //设置填充方式
//        distribution = .fillEqually
        // 设置布局方式
        axis = .horizontal
        // 填充方式
        distribution = .fillEqually
        
        addButton(text: "最近", imgName: "compose_emotion_table_left", type: .recent)
        addButton(text: "默认", imgName: "compose_emotion_table_mid", type: .normal)
        addButton(text: "Emoji", imgName: "compose_emotion_table_mid", type: .emoji)
        addButton(text: "浪小花", imgName: "compose_emotion_table_right" , type:.lxh )
        
    }
    
    
    func addButton(text:String, imgName:String, type:HMExpressionBottomBtnType) -> Void {
    

        let btn = UIButton()
        btn.tag = type.rawValue
        //如果是第二个button就设置成选中状态
        if type == .normal {
            
            btn.isSelected = true
            ///记录按钮
            self.selectdBtn = btn
        }
        btn.addTarget(self, action: #selector(expressionBottomBtnClick), for: UIControlEvents.touchUpInside)
   
        btn.setBackgroundImage(UIImage(named:"\(imgName)_normal"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"\(imgName)_selected"), for: UIControlState.selected)
        
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.selected)
        btn.setTitle(text, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: midTextFont)
        addArrangedSubview(btn)
        
    }
   
    // MARK: 底部四个按钮的点击事件
    func expressionBottomBtnClick(button:UIButton) -> Void {
        
        //判断方式二 当前点击按钮是否为选中按钮 如果是 就return
        if selectdBtn == button {
            return
        }
        
        //判断方式一
//        if self.selectdBtn?.tag == button.tag {
//            return
//        }
        
        button.isSelected = true
        self.selectdBtn?.isSelected = false
        self.selectdBtn = button
    }
    
    
}
